package _0881_Boats_to_Save_People;

import java.util.Arrays;

/**
 * Date: 2021/8/26 9:49
 * 执行用时：16 ms, 在所有 Java 提交中击败了95.52%的用户
 * 内存消耗：47.4 MB, 在所有 Java 提交中击败了45.42%的用户
 */
public class Solution {

    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);

        int res = 0;
        for (int i = people.length - 1, j = 0; j <= i; i--) {
            if (j < i && people[i] + people[j] <= limit) j++;
            res++;
        }
        return res;
    }

    public static void main(String[] args) {

        int[] people = {3, 2, 2, 1};
        System.out.println((new Solution()).numRescueBoats(people, 3));  // 3
    }
}
